package testrank;

import java.io.Serializable;
import java.util.List;

import testrank.affinity.SignatureParser;
import testrank.affinity.WordGroup;

/**
 * Information on a code element, typically a method
 * 
 * @author Hagai Cibulski
 */
public class ElementInfo implements Serializable {

	private static final long serialVersionUID = 1L;

	private String signature;
	
	private int beginLocation;
	
	private int endLocation;
	
	private WordGroup wordGroup;

	public ElementInfo(String signature, int beginLocation, int endLocation) {
		super();
		this.signature = signature;
		this.beginLocation = beginLocation;
		this.endLocation = endLocation;
	}


	/**
	 * Enriches the wordGroup of this ElementInfo with words in entities outside the method
	 * (typically class name words)
	 */
	public void enrichWordGroup() {
		SignatureParser methodSigParser = new SignatureParser(signature);
		List<String> classNameWords = methodSigParser.getClassNameWords();
		for (String word : classNameWords) {
			wordGroup.addWord(word);	
		}
		String classNameLiteral = methodSigParser.getClassNameLiteral();
		wordGroup.addLiteral(classNameLiteral);	
	}

	/**
	 * @return the signature
	 */
	public String getSignature() {
		return this.signature;
	}

	/**
	 * @param signature the signature to set
	 */
	public void setSignature(String signature) {
		this.signature = signature;
	}

	/**
	 * @return the beginLocation
	 */
	public int getBeginLocation() {
		return this.beginLocation;
	}

	/**
	 * @param beginLocation the beginLocation to set
	 */
	public void setBeginLocation(int beginLocation) {
		this.beginLocation = beginLocation;
	}

	/**
	 * @return the endLocation
	 */
	public int getEndLocation() {
		return this.endLocation;
	}

	/**
	 * @param endLocation the endLocation to set
	 */
	public void setEndLocation(int endLocation) {
		this.endLocation = endLocation;
	}
	
	public WordGroup getWordGroup() {
		return this.wordGroup;
	}

	public void setWordGroup(WordGroup wordGroup) {
		this.wordGroup = wordGroup;
	}

	@Override
	public String toString() {
		return "ElementInfo [signature="
				+ this.signature + ", beginLocation=" + this.beginLocation
				+ ", endLocation=" + this.endLocation + ", wordGroup=" + this.wordGroup + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + this.beginLocation;
		result = prime * result + this.endLocation;
		result = prime * result
				+ ((this.signature == null) ? 0 : this.signature.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ElementInfo other = (ElementInfo) obj;
		if (this.beginLocation != other.beginLocation)
			return false;
		if (this.endLocation != other.endLocation)
			return false;
		if (this.signature == null) {
			if (other.signature != null)
				return false;
		} else if (!this.signature.equals(other.signature))
			return false;
		return true;
	}
}
